package com.jx.driving.request.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;

import com.jx.driving.protocol.terminal.annotation.DwordAnnotation;
import com.jx.driving.protocol.terminal.annotation.DynamicByteArrayAnnotation;
import com.jx.driving.protocol.terminal.annotation.TerminalAnnotation;
import com.jx.driving.request.domain.ParameterIdDefinition.Types;

/**
 * 参数
 * 
 * @author shenliuyang
 *
 *         2016年8月12日下午4:40:54
 */
@TerminalAnnotation
public class Parameter {

	@DwordAnnotation(order = 1)
	private long id;// 参数id
	@DynamicByteArrayAnnotation(value = 1, order = 2)
	private byte[] value;
	
	
	public Parameter(long id, int value) {
		super();
		this.id = id;
		setInteger(value);
	}
	
	public Parameter(long id, short value) {
		super();
		this.id = id;
		setShort(value);
	}

	public Parameter() {
		super();
	}

	public Parameter(long id, String value) {
		super();
		this.id = id;
		setString(value);
	}

	public Parameter(long id, long value) {
		super();
		this.id = id;
		setLong(value);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getString() {
		return new String(value).trim();
	}

	public void setString(String value) {
		StringBuilder sb = new StringBuilder(value);
		sb.append('\0');
		this.value = sb.toString().getBytes();
	}

	public long getLong() {
		int ret = 0;
		for (int i = 0; i < 4; i++) {
			ret <<= 8;
			ret |= value[i] & 0xFF;
		}
		return ret;
	}

	public void setLong(long value) {
		ByteBuffer bb = ByteBuffer.allocate(4);
		bb.putInt((int) value);
		this.value = bb.array();
	}

	public int getInteger() {
		int ret = 0;
		for (int i = 0; i < 2; i++) {
			ret <<= 8;
			ret |= value[i] & 0xFF;
		}
		return ret;
	}

	public void setInteger(int value) {
		ByteBuffer bb = ByteBuffer.allocate(2);
		bb.putShort((short) value);
		this.value = bb.array();
	}

	public short getShort() {
		return (short) (value[0] & 0xFF);
	}

	public void setShort(short value) {
		ByteBuffer bb = ByteBuffer.allocate(1);
		bb.put((byte) value);
		this.value = bb.array();
	}

	public Object getObject() {
		ParameterIdDefinition p = ParameterIdDefinition.getById(id);
		if (Types.STRING.equals(p.getType())) {
			return getString();
		} else if (Types.LONG.equals(p.getType())) {
			return getLong();
		} else if (Types.INTEGER.equals(p.getType())) {
			return getInteger();
		} else {
			return getShort();
		}
	}

	public void setObject(Object value) {
		ParameterIdDefinition p = ParameterIdDefinition.getById(id);
		if (Types.STRING.equals(p.getType())) {
			setString(value.toString());
		} else if (Types.LONG.equals(p.getType())) {
			setLong((Long) value);
		} else if (Types.INTEGER.equals(p.getType())) {
			setInteger((Integer) value);
		} else {
			setShort((Short) value);
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Parameter [id=").append(id).append(", value=").append(Arrays.toString(value)).append("]");
		return builder.toString();
	}

}
